<script setup>
import FilterBar from '@/components/FilterBar.vue'
import { onMounted, inject, reactive, nextTick } from 'vue'

const $http = inject('$http')

const dataObj = reactive({
  list: [],
  selectValue: 'day',
})

onMounted(() => {
  getChartData()
})

function getChartData() {
  $http
    .get('/screen/person/from-city', { params: { scope: dataObj?.selectValue } })
    .then(res => {
      const { data = {} } = res || {}

      dataObj.list = data || []
    })
    .then(nextTick)
    .then(() => {
      autoScroll('table_natest_contiguitydata1')
      autoScroll('table_natest_contiguitydata2')
    })
}

// 列表滚动方法
function autoScroll(id) {
  const el = document.getElementById(id)
  const elHeight = el.offsetHeight
  const sHeight = el.scrollHeight

  setInterval(() => {
    if (el.scrollTop < sHeight - elHeight) {
      el.scrollTop++
    } else {
      el.scrollTop = 0
    }
  }, 100)
}

function change(item = {}) {
  dataObj.selectValue = item?.value || ''

  getChartData()
}
</script>

<template>
  <div class="originAnalysisBox">
    <div class="title">返乡人员来源地分析</div>

    <FilterBar class="filterBox" :selectValue="dataObj.selectValue" @change="change" />

    <div class="tableCenter">
      <div class="tableLeft">
        <div class="title">省内返乡</div>

        <div class="tableBox">
          <div class="line">
            <div style="flex: 0.5">序号</div>
            <div style="flex: 1">地市名称</div>
            <div style="flex: 1">返乡人数</div>
          </div>
          <div class="table scroll" id="table_returntown_originanalysis1">
            <div class="tr" v-for="(item, index) in dataObj.list?.province || []" :key="index" ref="tableRef">
              <div style="flex: 0.5">{{ index + 1 }}</div>
              <div style="flex: 1">{{ item.label }}</div>
              <div style="flex: 1">{{ item.value }}</div>
            </div>
          </div>
        </div>
      </div>

      <div class="tableRight">
        <div class="title">省外返乡</div>

        <div class="tableBox">
          <div class="line">
            <div style="flex: 0.5">序号</div>
            <div style="flex: 1">地市名称</div>
            <div style="flex: 1">返乡人数</div>
          </div>
          <div class="table scroll" id="table_returntown_originanalysis2">
            <div
              class="tr"
              v-for="(item, index) in dataObj.list?.otherProvince || []"
              :key="index"
              ref="tableRef"
            >
              <div style="flex: 0.5">{{ index + 1 }}</div>
              <div style="flex: 1">{{ item.label }}</div>
              <div style="flex: 1">{{ item.value }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.originAnalysisBox {
  width: 756px;
  height: 662px;
  background-image: url('@/assets/returnTown/boxbg1.png');
  background-repeat: no-repeat;
  background-size: 100% 100%;
  display: flex;
  flex-direction: column;

  .title {
    margin-top: 8px;
    width: 100%;
    height: 20px;
    font-size: 14px;
    color: #ffffff;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .filterBox {
    margin: 13px 16px 12px 16px;
  }

  .tableCenter {
    flex: 1;
    display: flex;
    overflow: hidden;
    padding: 0 16px 26px 16px;

    .tableLeft,
    .tableRight {
      flex: 1;
      display: flex;
      flex-direction: column;

      .title {
        width: 354px;
        height: 20px;
        background: rgba(19, 181, 177, 0.4);
        display: flex;
        justify-content: center;
        align-items: center;
      }

      .tableBox {
        flex: 1;
        display: flex;
        flex-direction: column;
        overflow: hidden;

        .line {
          display: flex;
          flex-direction: row;
          justify-content: center;
          width: 100%;
          height: 20px;
          font-size: 14px;
          color: #ffffff;
          margin-top: 12px;

          div {
            display: flex;
            justify-content: center;
          }

          .tag {
            width: 32px;
            height: 40px;

            &.yxTag {
              width: 80px;
            }
          }
        }
        .table {
          flex: 1;
          width: 100%;
          overflow: hidden auto;
          margin-top: 16px;

          .tr {
            display: flex;
            flex-direction: row;
            justify-content: center;
            align-items: center;
            height: 56px;
            font-size: 14px;
            color: #ffffff;
            text-align: center;

            &:nth-child(odd) {
              background: rgba(19, 181, 177, 0.4);
            }
          }
        }
      }
    }

    .tableRight {
      margin-left: 16px;
      .title,
      .tableBox .table .tr:nth-child(odd) {
        background: rgba(28, 66, 164, 0.4);
      }
    }
  }
}
</style>
